package com.yimicloud.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/**
 * Created by justin on 15/9/8.
 */
public class JsonUtil {
    private Map<String, String> jsonMap = new HashMap();
    private static Map<String, Object> map = null;
    public static final String STATUS = "status";
    public static final String MESSAGE = "message";
    public static final String SUCCESS = "success";
    public static final String FAILURE = "failure";
    public static final String EXCEPITON = "exception";
    public static final String ERROR = "error";
    public static final String ADDRESS = "address";

    public JsonUtil() {
    }

    public static String getJsonKey(String json, String key) {
        try {
            if(json.charAt(0) != 91) {
                json = "[" + json;
            }

            if(json.charAt(json.length() - 1) != 93) {
                json = json + "]";
            }

            JSONArray e = JSONArray.parseArray(json);
            if(e.size() > 0) {
                JSONObject jsonObject = e.getJSONObject(0);
                return (String)jsonObject.get(key);
            }
        } catch (Exception var4) {
            System.out.println("json:" + json);
            var4.printStackTrace();
        }

        return null;
    }

    public static String editJsonKey(String json, String key, String value) {
        map = JsonToMap(json);
        if(map.get(key) != null) {
            map.put(key, value);
        }

        String result = mapTojsonText(map);
        map = null;
        return result;
    }

    public static String editJsonKey(String json, String key, int value) {
        map = JsonToMap(json);
        if(map.get(key) != null) {
            map.put(key, Integer.valueOf(value));
        }

        String result = mapTojsonText(map);
        map = null;
        return result;
    }

    public static String editJsonKey(String json, String key, double value) {
        map = JsonToMap(json);
        if(map.get(key) != null) {
            map.put(key, Double.valueOf(value));
        }

        String result = mapTojsonText(map);
        map = null;
        return result;
    }

    public static Map<String, Object> JsonToMap(String json) {
        if(json != null && !"".equals(json)) {
            try {
                HashMap e = new HashMap();
                if(json.charAt(0) != 91) {
                    json = "[" + json;
                }

                if(json.charAt(json.length() - 1) != 93) {
                    json = json + "]";
                }

                JSONArray arry = JSONArray.parseArray(json);
                if(arry.size() > 0) {
                    JSONObject jsonObject = arry.getJSONObject(0);
                    Iterator var5 = jsonObject.keySet().iterator();

                    while(true) {
                        while(var5.hasNext()) {
                            String key = (String)var5.next();
                            String value = jsonObject.getString(key);
                            if(value != null && !"null".equals(value)) {
                                e.put(key, value);
                            } else {
                                e.put(key, "");
                            }
                        }

                        return e;
                    }
                } else {
                    return e;
                }
            } catch (Exception var7) {
                var7.printStackTrace();
                return null;
            }
        } else {
            return new HashMap();
        }
    }

    public void jsonPut(String key, String value) {
        this.jsonMap.put(key, value);
    }

    public void jsonPut(String key, int value) {
        this.jsonMap.put(key, String.valueOf(value));
    }

    public void jsonPut(String key, double value) {
        this.jsonMap.put(key, String.valueOf(value));
    }

    public void jsonPutStatus(String value) {
        this.jsonMap.put("status", value);
    }

    public void jsonPutStatus(int value) {
        this.jsonMap.put("status", String.valueOf(value));
    }

    public void jsonPutStatus(double value) {
        this.jsonMap.put("status", String.valueOf(value));
    }

    public String success() {
        this.jsonMap.put("status", "success");
        return this.getJsonText();
    }

    public static String successJson() {
        map = new HashMap();
        map.put("status", "success");
        return mapTojsonText(map);
    }

    public String failure() {
        this.jsonMap.put("status", "failure");
        return this.getJsonText();
    }

    public static String failureJson() {
        map = new HashMap();
        map.put("status", "failure");
        return mapTojsonText(map);
    }

    public String error() {
        this.jsonMap.put("status", "error");
        return this.getJsonText();
    }

    public static String exceptionJson() {
        map.put("status", "exception");
        map.put("message", "操作异常!");
        return mapTojsonText(map);
    }

    public void jsonPutMessage(String value) {
        this.jsonMap.put("message", value);
    }

    public void jsonPutMessage(int value) {
        this.jsonMap.put("message", String.valueOf(value));
    }

    public void jsonPutMessage(double value) {
        this.jsonMap.put("message", String.valueOf(value));
    }

    public String getJsonText() {
        if(this.jsonMap != null && this.jsonMap.size() > 0) {
            StringBuffer jsonText = new StringBuffer("{");
            Iterator var3 = this.jsonMap.keySet().iterator();

            while(var3.hasNext()) {
                String key = (String)var3.next();
                jsonText.append("\"");
                jsonText.append(key);
                jsonText.append("\"");
                jsonText.append(":");
                jsonText.append("\"");
                jsonText.append((String)this.jsonMap.get(key));
                jsonText.append("\"");
                jsonText.append(",");
            }

            jsonText.deleteCharAt(jsonText.length() - 1);
            jsonText.append("}");
            return jsonText.toString();
        } else {
            return null;
        }
    }

    public static String mapTojsonText(Map<String, Object> map) {
        if(map == null) {
            return null;
        } else {
            StringBuffer jsonText = new StringBuffer("{");
            Iterator var3 = map.keySet().iterator();

            while(var3.hasNext()) {
                String key = (String)var3.next();
                jsonText.append("\"");
                jsonText.append(key);
                jsonText.append("\"");
                jsonText.append(":");
                jsonText.append("\"");
                jsonText.append(map.get(key));
                jsonText.append("\"");
                jsonText.append(",");
            }

            jsonText.deleteCharAt(jsonText.length() - 1);
            jsonText.append("}");
            return jsonText.toString();
        }
    }

    public static void main(String[] args) {
        System.out.println(editJsonKey("{\'name\':\'123\',\'id\':\'ddd\'}", "id", "3"));
    }
}
